package com.angis.fx.adapter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;

import com.angis.fx.activity.R;

public class MultipleAdapter extends BaseAdapter {

	private List<String> mDataList;
	private LayoutInflater mLayoutInflater;
	private Set<Integer> mPos;

	public MultipleAdapter(Activity pParamActivity) {
		super();
//		lPos = new HashSet<Integer>();
		this.mLayoutInflater = LayoutInflater.from(pParamActivity);
		mDataList = new ArrayList<String>();
	}

	@Override
	public int getCount() {
		return mDataList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	public void setCheckedPositions(Set<Integer> pPos){
		mPos = pPos;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder lHolder = null;
		if (convertView == null) {
			lHolder = new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.multiple_dialog_list_item, null);
			lHolder.mName = (TextView) convertView.findViewById(R.id.multiple_dialog_item_name);
			lHolder.mCheckText = (CheckedTextView) convertView.findViewById(R.id.multiple_dialog_item_ctv);
			convertView.setTag(lHolder);
		} else {
			lHolder = (ViewHolder) convertView.getTag();
		}
		lHolder.mName.setText(mDataList.get(position));
		lHolder.mCheckText.setVisibility(View.VISIBLE);
		lHolder.mCheckText.setChecked(false);
		Iterator<Integer> lIt = mPos.iterator();
		while (lIt.hasNext()){
			if (lIt.next() == position){
				lHolder.mCheckText.setChecked(true);
				break;
			}
				
		}
		lHolder.mCheckText.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CheckedTextView lCheckTextView = ((CheckedTextView)v);
				if(lCheckTextView.isChecked()){
					mPos.remove(position);
					lCheckTextView.setChecked(false);
				}else if(!lCheckTextView.isChecked()){
					mPos.add(position);
					lCheckTextView.setChecked(true);
				}
			}
		});
//		if (mPos != null && mPos.size()>0){
//			Iterator<Integer> lIt = mPos.iterator();
//			while (lIt.hasNext()){
//				if (lIt.next().intValue() == position){
//					lHolder.mCheckText.setChecked(true);
//					return convertView;
//				}
//			}
//		}
		return convertView;
	}

	public class ViewHolder {

		TextView mName;
		CheckedTextView mCheckText;

		public TextView getName() {
			return mName;
		}

		public CheckedTextView getCheckText() {
			return mCheckText;
		}

	}

	public void setData(List<String> pDataList, Set<Integer> pPos) {
		this.mDataList = pDataList;
		this.mPos = pPos;
	}

}
